package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.qo.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper ;

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRoleAndPermission(id);
    }

    @Override
    public void update(Role role,Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        //先把之前中间表数据全删了
        roleMapper.deleteRoleAndPermission(role.getId());
        if (ids != null&&ids.length>0){
            roleMapper.insertRoleAndPermission(role.getId(),ids);
        }
    }

    @Override
    public void insert(Role role,Long[] ids) {
        roleMapper.insert(role);
        if (ids != null&&ids.length>0){
            roleMapper.insertRoleAndPermission(role.getId(),ids);
        }
    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo<Role> query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        return new PageInfo<>(roleMapper.selectRole(queryObject));
    }

    @Override
    public List<Role> selectEmpAndRole(Long empId) {
        return roleMapper.selectEmpAndRole(empId);
    }

    @Override
    public List<Role> selectPermission(Long rid) {
        return roleMapper.selectPermission(rid);
    }


}
